package com.aierkeji.call.cms.controller;

import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aierkeji.call.cms.service.AkjCallB2cService;
import com.aierkeji.call.cms.service.AkjCallReportService;
import com.aierkeji.call.cms.util.ResultMapHelper;
import com.aierkeji.call.cms.util.WebUtils;

@RestController
@RequestMapping("/report")
public class AkjCallReportController {
	private final Logger log = LogManager.getLogger(getClass());
	@Autowired
	AkjCallReportService akjCallReportService;

	@Autowired
	AkjCallB2cService akjCallB2cService;

	@PostMapping("/table/list/{pageNo}")
	public Map<String, Object> tableReport(@PathVariable(name = "pageNo") int pageNo,
			@RequestBody Map<String, Object> map) {
		return akjCallReportService.report(pageNo, map, true);
	}

	@PostMapping("/chart/list/{pageNo}")
	public Map<String, Object> chartReport(@PathVariable(name = "pageNo") int pageNo,
			@RequestBody Map<String, Object> map) {
		return akjCallReportService.report(pageNo, map, false);
	}

	@PostMapping("/getQueryConfig")
	public Map<String, Object> getQueryConfig(@RequestBody Map<String, Object> map) {
		return akjCallReportService.getQueryConfig();
	}

	@GetMapping("/down")
	public ResponseEntity down(HttpServletRequest request) throws Exception {
		byte[] bytes = null;
		HttpStatus statusCode = HttpStatus.OK;
		HttpHeaders headers = new HttpHeaders();
		Map<String, Object> resultMap = null;
		try {
			resultMap = akjCallReportService.down(WebUtils.getParamsMap(request));
		} catch (Exception e) {
			log.error("导出下级统计列表错误:", e);
			headers.setContentType(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8"));
			ResponseEntity<String> entity = new ResponseEntity<String>("发生未知错误请联系管理员!", headers, statusCode);
			return entity;
		}
		if (ResultMapHelper.isSuccess(resultMap)) {
			bytes = resultMap.get("data").toString().getBytes("GBK");
		} else {
			headers.setContentType(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8"));
			ResponseEntity<String> entity = new ResponseEntity<String>((String) resultMap.get("msg"), headers,
					statusCode);
			return entity;
		}
		headers.add("Content-Disposition", "attchement;filename=" + URLEncoder.encode("下级统计列表", "UTF-8") + ".csv");
		ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(bytes, headers, statusCode);
		return entity;

	}

	@PostMapping("/callB2bUpdateStatistics")
	public Map<String, Object> callB2bUpdateStatistics(@RequestBody Map<String, Object> map) {
		Map<String, Object> resultMap = akjCallB2cService.updateStatistics(map);
		if (resultMap == null) {
			resultMap = ResultMapHelper.failure("通知更新失败！");
		}else if(ResultMapHelper.isSuccess(resultMap)) {
			resultMap = ResultMapHelper.success(null,"通知更新统计数据成功，正在更新...");
		}
		
		return resultMap;
	}
}
